<?php
namespace app\common\library;

use think\Log as tLog;
use app\admin\model\partner\level\Setting;


class Partner
{
    public function getSubPartners($partner) 
    {
        $subRows = \app\admin\model\user\Partner::where('dre_id', $partner->id)->where('status', 2)->select();
        $subPartners = [];
        foreach ($subRows as $sr) {
            if (!isset($subPartners[$sr['level_id']])) {
                $subPartners[$sr['level_id']] = 1;
            } else {
                $subPartners[$sr['level_id']] += 1;
            }
        }
        return $subPartners;
    }

    public function getReachDetail($upgraderule, $subPartners)
    {
        $maxLevelId = \app\admin\model\user\Partner::getMaxLevelId();
        foreach ($upgraderule as &$r) {
            if ($r['type'] == 1 && !empty($r['reach_detail'])) {
                $reachDetail = json_decode($r['reach_detail'], true);
                $reachDetail = array_map(function ($e) use($subPartners, $maxLevelId) {
                  $amount = intval($e['amount']);
                  $levelId = intval($e['level_id']);
                  $reachAmount = 0;
                  $isReach = 0;
                  // 大于等于已设置的等级
                  for($lid = $maxLevelId; $lid <= $levelId; $lid++) {
                      if (isset($subPartners[$lid])) {
                          $reachAmount += $subPartners[$lid];
                      }
                  }
                  // 子项是否达标
                  if ($reachAmount >= $amount) {
                      $isReach = 1;
                  }
                  $e['amount'] = $amount;
                  $e['is_reach'] = $isReach;
                  $e['reach_amount'] = $reachAmount; // 子项达标人数
                  $e['reach_desc'] = sprintf('带领%s位以上兄弟姐妹共创财富，已完成%s人', $amount, $reachAmount);
                  return $e;
                }, $reachDetail);
            } else {
                $reachDetail = [];
            }
            $r['reach_detail'] = $reachDetail;
        }
        unset($r);

        return $upgraderule;

    }

    public function updateParentId($partnerId)
    {
        $partner = \app\admin\model\user\Partner::get($partnerId);
        if (!empty($partner->parent_id)) {
            $upLevelId = $partner->level_id;
            $upLevelObj = Setting::get($upLevelId);

            // 20250113修改为 我得上级批发商：统一显示为同一条推荐线上级别高于我且离我最近得批发商
            $parentIdPathArr = explode(",", $partner->dre_id_path);

            // 查找所有同一条推荐线上的人的用户的级别
            $where = [
                'id' => ['in', $parentIdPathArr],
            ];
            $parentPartnersObj = \app\admin\model\user\Partner::where($where)->order('id', 'desc')->select();
            $parentPartners = [];
            foreach ($parentPartnersObj as $p) {
                $parentPartners[$p->id] = $p->level->level;
            }
            $gePartnerId = null;
            // 从路径开始找
            foreach ($parentIdPathArr as $parentId) {
                // 等级数值越小，等级越大的最近的上级批发商
                if (isset($parentPartners[$parentId]) && $parentPartners[$parentId] < $upLevelObj->level) {
                    $gePartnerId = $parentId;
                    break;
                }
            }
            $msg = vsprintf("被动更新上级ID partner id: %s, 级别ID: %s, 定义级别值:%s, 重新查找的parent id: %s", [$partner->id, $upLevelId, $upLevelObj->level, $gePartnerId]);
            // echo $msg . "<br />";
            tLog::write($msg);
            if ($partner->parent_id != $gePartnerId) {
                $partner->parent_id = $gePartnerId;
                $partner->save();
            }
        }
    }
}
